home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1080 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.sni.de!news
  2. From: Josef Moellers <mollers.pad@sni.de>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help on Dynamically Allocating an Array?
  5. Date: 11 Jan 1996 08:06:08 GMT
  6. Organization: Siemens Nixdorf Informationssysteme AG, Paderborn, Germany
  7. Message-ID: <4d2gdg$t3m@nervous.pdb.sni.de>
  8. References: <4cq273$aeq@mercury.IntNet.net>
  9. NNTP-Posting-Host: uranium.pdb.sni.de
  10. X-Newsreader: NN version 6.5.0 #2
  11.  
  12. In <4cq273$aeq@mercury.IntNet.net> jtomich@IntNet.net (Jeff Tomich) writes:
  13.  
  14. >I'm confused on who to dynamically allocate an array of any type. Any 
  15. >help would be much appreciated.
  16.  
  17. Either
  18.  
  19. type *pointer;
  20. ...
  21. pointer = (type *) malloc(nitems * sizeof(type));
  22.  
  23. or
  24.  
  25. ...
  26. pointer = (type *) calloc(nitems, sizeof(type));
  27.  
  28.  
  29. The latter explicitly initializes the array to all zeroes, while the
  30. former only does this (implicitly) if the memory for the array will have
  31. to be obtained by asking the OS for it. In that case, the OS will
  32. usually clear whatever memory it gives your process to avoid security
  33. problems.
  34.  
  35. Access in both cases is
  36.  
  37. pointer[index]
  38.  
  39. Hope this helps,
  40.  
  41. Josef
  42. --
  43. Josef Moellers            mollers.pad@sni.de
  44.